var vm = new Vue({
    el: '#app',
    data: {
        specGroup: '' ,
        specParam: '',
        groupId: ''
    },
    created(){
        // this.getBrand();

    },
    mounted(){
        this.getTreeData();
    },
    methods:{
        //初始化树
        getTreeData(){

            $('#categoryTree').tree({
                url:'category/list',
                method:'get',
                // checkbox: true,
                lines: true,
                animate:true,
                formatter: function (data) {
                    return data.name;
                }
            });


        },

        // 表格内点击事件
        getSpecParam(index){

            // 表格当前行id
            this.groupId = this.specGroup[index].id;

            $.get('spec/querySpecParam', {group_id: this.groupId},function (res) {

                vm.specParam = res;
            });
        },

        // 分组删除事件
        deleteGroup(index){

            // 表格当前行id
            var id = this.specGroup[index].id;

            console.log(id)

            $.ajax({
                url: 'spec/moveSpecGroup',
                type: 'post',
                data: {
                    id: id
                },
                dataType:"json",
                success: function (res) {

                }
            })
        }
    },
    filters:{
        numericFormat: function (str) {

            if (str == true){
                return "是";
            } else {
                return "否";
            }
        },
        unitFormat: function (str) {
            if (str == ""){
                return "无";
            } else {
                return str;
            }
        },
        genericFormat: function (str) {
            if (str == true){
                return "是";
            } else {
                return "否";
            }
        },
        searchingFormat: function (str) {
            if (str == true){
                return "是";
            } else {
                return "否";
            }
        }
    }
});

var cid;

// 树控件选择事件
$('#categoryTree').tree({
    onSelect: function (node) {

        cid = node.id;

        $.get('spec/querySpecGroup', {cid: node.id},function (res) {

            vm.specGroup = res;
        });
    }
});

// bootstrap 模态框 点击添加 显示弹出框
$('#addGroupForm').on('show.bs.modal', function (event) {

    $('#add_group_submit').click(function () {

        var name = $('#addGroupName').val();

        $.post('spec/addGroup', {cid: cid, name: name}, function (res) {


        });

        $('#addGroupForm').modal('hide');
    });

});

// bootstrap 模态框 点击修改 显示弹出框
$('#modifyGroupForm').on('show.bs.modal', function (event) {

    var groupId = event.relatedTarget.attributes.data.nodeValue;
    var groupName = event.relatedTarget.attributes.name.nodeValue;

    $('#modifyGroupName').val(groupName);

    $('#modify_group_submit').click(function () {

        var name = $('#modifyGroupName').val();

        $.post('spec/modifyGroup', {id: groupId, name: name}, function (res) {


        });

        $('#modifyGroupForm').modal('hide');
    });

});


// bootstrap 模态框 点击添加 显示弹出框
$('#addParamForm').on('show.bs.modal', function (event) {

    $('#add_param_submit').click(function () {

        var name = $('#addParamName').val();

        var common = $("[name='form-common-checkbox']").prop("checked");
        var num = $("[name='form-num-checkbox']").prop("checked");
        var search = $("[name='form-search-checkbox']").prop("checked");

        var generic;
        var numeric;
        var searching;

        var addNum;
        var from_to;
        common == true ? generic = 1 : generic = 0;

        if (num == true){
            numeric = 1;

            addNum =  $('#addNum').val();
        } else {
            numeric = 0;
            addNum = "";
        }

        if (search == true){
            searching = 1;
            from_to = $('#add_search_from').val() + "-" + $('#add_search_to').val();
        } else {
            searching = 0;
        }

        $.post('spec/addParam', {
            name: name,
            cid: cid,
            group_id: vm.groupId,
            generic: generic,
            numeric: numeric,
            unit: addNum,
            searching: searching,
            segments: from_to
        },function (res) {

        });

        $('#modifyGroupForm').modal('hide');
    });

});

// bootstrap 模态框 点击修改 显示弹出框
$('#modifyParamForm').on('show.bs.modal', function (event) {

    var paramId = event.relatedTarget.attributes.data.nodeValue;
    var paramName = event.relatedTarget.attributes.name.nodeValue;

    var paramNumeric;

    if(event.relatedTarget.attributes.numeric == undefined){
        paramNumeric = false;
    }else {
        paramNumeric = true;
    }
    var paramUnit = event.relatedTarget.attributes.unit.nodeValue;
    var paramGeneric;
    if(event.relatedTarget.attributes.generic == undefined){
        paramGeneric = false;
    }else {
        paramGeneric = true;
    }
    var paramSearching;
    if(event.relatedTarget.attributes.searching == undefined){
        paramSearching = false;
    }else {
        paramSearching = true;
    }

    $('#modifyParamName').val(paramName);

    if (paramGeneric){

        $('#form-common-checkbox').attr("checked",true);
    }
    if (paramNumeric){

        $('#form-num-checkbox').attr("checked", true);
        $('#modify_num_form').css('display', 'block');
        $('#modifyNum').val(paramUnit);

    }
    if (paramSearching){

        $('#form-search-checkbox').attr("checked", true);
        $('#modify_search_form').css('display', 'block');
    }

});

// 隐藏弹出框
$('#modifyParamForm').on('hidden.bs.modal', function (e) {
    $('#form-common-checkbox').attr("checked", false);
    $('#form-num-checkbox').attr("checked", false);
    $('#modify_num_form').css('display', 'none');
    $('#form-search-checkbox').attr("checked", false);
    $('#modify_search_form').css('display', 'none');
});

function add_num(num) {

    var check = num.checked;

    if (check){
        $('#add_num_input').css('display', 'block')

    }else {
        $('#add_num_input').css('display', 'none')
    }
}

function add_search(search) {
    var check = search.checked;

    if (check){
        $('#add_search_input').css('display', 'block')
    }else {
        $('#add_search_input').css('display', 'none')
    }
}

function modify_num(num) {

    var check = num.checked;

    if (check){
        $('#modify_num_form').css('display', 'block')
    }else {
        $('#modify_num_form').css('display', 'none')
    }
}

function modify_search(search) {
    var check = search.checked;

    if (check){
        $('#modify_search_form').css('display', 'block')
    }else {
        $('#modify_search_form').css('display', 'none')
    }
}



